//
//  PayData.m
//  TestJSPay
//
//  Created by hongliang on 16/3/14.
//  Copyright © 2016年 hongliang. All rights reserved.
//

#import "WXPayData.h"

@implementation WXPayData
+ (id)parseObjectFormJsonObject:(id)jsonObject {
    if (![jsonObject isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    NSDictionary* dictResult = (NSDictionary*)jsonObject;
    NSString* result = [self stringValue:[dictResult objectForKey:@"resultCode"] defaultValue:@""];
    if ([result isEqualToString:@"200"]) {
        NSDictionary* dictContent = [dictResult objectForKey:@"result"];
        if ([dictContent isKindOfClass:[NSDictionary class]]) {
            WXPayData* payData = [[WXPayData alloc]init];
            payData.tradeNo = [self stringValue:[dictResult objectForKey:@"tradeNo"] defaultValue:@""];
            for (NSString* strKey in dictContent) {
                [payData setValue:[self stringValue:[dictContent objectForKey:strKey] defaultValue:@""] forKey:strKey];
            }
            return payData;
        }
    }
    
    return nil;
}

+ (BOOL)parseWeChatPayPayResp:(PayResp*)res error:(NSError**)error {
    BOOL isSuccess = NO;
    NSString* strMessage = nil;
    switch (res.errCode) {
        case WXSuccess:
            strMessage = @"订单支付成功";
            isSuccess = YES;
            break;
        case WXErrCodeCommon:
            strMessage = @"订单支付失败";
            break;
        case WXErrCodeSentFail:
            strMessage = @"订单支付失败";
            break;
        case WXErrCodeUserCancel:
            strMessage = @"用户取消";
            break;
        case WXErrCodeAuthDeny:
            strMessage = @"授权失败";
            break;
        case WXErrCodeUnsupport:
            strMessage = @"微信不支持";
            break;
        default:
            break;
    }
    
    *error = [NSError errorWithDomain:@"jsPay.WeChatPayResult" code:res.errCode userInfo:@{NSLocalizedDescriptionKey:strMessage}];
    return isSuccess;
    
    return NO;
}

+ (NSString*)payOrderStaus:(NSDictionary*)dictResult {
    if ([dictResult isKindOfClass:[NSDictionary class]]) {
        NSArray* arrayResult = [dictResult objectForKey:@"result"];
        if ([arrayResult isKindOfClass:[NSArray class]] && arrayResult.count > 0) {
            NSDictionary* dictResult0 = [arrayResult objectAtIndex:0];
            if ([dictResult0 isKindOfClass:[NSDictionary class]]) {
                return [HLBaseData stringValue:[dictResult0 objectForKey:@"status"] defaultValue:@""];
            }
        }
    }
    
    return nil;
}
@end
